# functions, classes and stuff for the toolbar


import towers
import common
import pygame

class UI:
    WIDTH = 144
    targetmodes = towers.Tower.targetmodes
    
    def __init__(self,x,user):
        
        self.title_box =        Box([612,4,136,25],(50,50,200))
        self.info_box =         Box([612,37,136,100],(50,50,200))
        self.twr_select_box =   Box([612,200,136,150],(50,50,200))
        self.twr_popup_box =    Box([612,400,100,192],(50,50,200))    
        self.scroll_box =       Box([716,388,32,216],(50,50,200))
        
        self.x = x
        self.user = user
        
        self.elements = []
        self.boxes = [self.title_box, self.info_box, self.twr_select_box,self.twr_popup_box]
        #self.popup = [self.twr_popup_box]
        
        self.font = pygame.font.Font(None, 20)
        self.font2 = pygame.font.Font(None,18)
        
    def set_popup(self,tower):
        self.twr_popup_box.elements = []
        if tower <> None:
            cost=Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+20),"Costs: "+str(tower.cost),(0,0,0),self.font2)
            self.twr_popup_box.elements.append(cost)
            name = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+40),tower.name,(0,0,0),self.font2)
            dmg = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+60),"Atk Dmg: " + str(tower.attack_dmg),(0,0,0),self.font2)
            sight = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+80),"Dist: " + str(tower.sight),(0,0,0),self.font2)
            speed = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+100),"Atk Spd: " + str(tower.reload_rate/40.0),(0,0,0),self.font2)
            try:
                effect = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+120),"Effect: " + str(tower.effect.etype),(0,0,0),self.font2)
                effectlen = Font((self.twr_popup_box.rect[0],self.twr_popup_box.rect[1]+132),"Length: " + str(tower.effect.length),(0,0,0),self.font2)
            except: pass
            
            self.twr_popup_box.elements.append(name)
            self.twr_popup_box.elements.append(sight)
            self.twr_popup_box.elements.append(dmg)
            self.twr_popup_box.elements.append(speed)
            try:
                self.twr_popup_box.elements.append(effect)
                self.twr_popup_box.elements.append(effectlen)
            except: pass   
                 
    def build(self,tower):
        #clear list        
        self.elements = []
        self.boxes = []       
        self.info_box.elements = []
        self.twr_select_box.elements = []
        self.title_box.elements = []
        #build info box
        
        money = Font((self.info_box.rect[0]+20,self.info_box.rect[1]+20),"MONEY: "+str(self.user.money),(0,0,0),self.font2)
        creeplvl = Font((self.info_box.rect[0]+20,self.info_box.rect[1]+40),"NEXT LEVEL: "+str(self.user.creeplvl),(0,0,0),self.font2)
        
        self.info_box.elements.append(money)
        self.info_box.elements.append(creeplvl)
        
        #Tower Button section        
        ypad = 8
        x,y = 716,400
        self.elements.append(up((x,y-12)))
        for n in range(0,5):
            self.elements.append(ui_btns[n]((x,y)))
            y += 32+ypad
            
        self.elements.append(down((x,y-4)))    
                       
        #Selected Tower section
        if tower<>None:
            spot = 0
            for mode in self.targetmodes:                
                pos = self.twr_select_box.rect[0]+((spot%3)*40)+((spot%3)*8),self.twr_select_box.rect[1]+((spot/3)*16)+((spot/3)*4)
                btn_mode = Target_Mode(pos,mode)
                self.twr_select_box.elements.append(btn_mode)                
                if mode == tower.targetmode:
                    outline = btn_mode.image.get_rect()
                    pygame.draw.rect(btn_mode.image,(0,255,255),outline,1)
                spot += 1
            
            name = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+40),tower.name,(0,0,0),self.font2)
            #description = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+60),tower.description,(0,0,0),self.fontsmall)
            dmg = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+60),"Attack Damage: " + str(tower.attack_dmg),(0,0,0),self.font2)
            sight = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+80),"Distance: " + str(tower.sight),(0,0,0),self.font2)
            speed = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+100),"Attack Speed: " + str(tower.reload_rate/40.0),(0,0,0),self.font2)
            try:
                effect = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+120),"Effect: " + str(tower.effect.etype),(0,0,0),self.font2)
                effectlen = Font((self.twr_select_box.rect[0],self.twr_select_box.rect[1]+132),"Length: " + str(tower.effect.length),(0,0,0),self.font2)
            except: pass
            
            self.twr_select_box.elements.append(name)
            self.twr_select_box.elements.append(sight)
            self.twr_select_box.elements.append(dmg)
            self.twr_select_box.elements.append(speed)
            try:
                self.twr_select_box.elements.append(effect)
                self.twr_select_box.elements.append(effectlen)
            except: pass
            
            pos = self.twr_select_box.rect[0]+self.twr_select_box.rect[2]- Sell.size[0], self.twr_select_box.rect[1]+self.twr_select_box.rect[3] - Sell.size[1]
            btn_sell = Sell(pos)
            self.twr_select_box.elements.append(btn_sell)
        
            # add boxes and stuff      
        self.boxes.append(self.twr_select_box)
        self.boxes.append(self.info_box)
        self.boxes.append(self.title_box)
        self.boxes.append(self.twr_popup_box)
        self.elements.extend(self.boxes) 
         
         
    def btn_scroll(self,bdir):
        if bdir == 'down':
            copy = ui_btns[0]
            ui_btns.remove(copy)
            ui_btns.append(copy)
            
        if bdir == 'up':
            copy = ui_btns[-1]
            ui_btns.remove(copy)
            ui_btns.insert(0,copy)
            
class Font:
    elem_type = "font"
    def __init__(self,pos,text,color,font):
        self.x,self.y = pos
        self.image = font.render(text,True,color)
        
class Box:
    elem_type = "box"
    def __init__(self,rect,color):
        self.rect = rect
        self.x,self.y = rect[0],rect[1]
        self.imageblank = pygame.Surface((rect[2],rect[3]))
        self.imageblank.fill(color)        
        self.image = self.imageblank.copy()
        self.elements = []
        
class Button:
    size = [32,32] # MAX SIZE FOR SIDEBAR BUTTONS, DOESNT REALLY MATTER UNLESS WE CHANGE PIXEL SIZES AND STUFF....
    elem_type = 'button'
    img_name = "btn"
        
    def __init__(self,pos):
        self.x,self.y = pos
        self.image = common.images[self.img_name].copy()
        self.rect = [self.x,self.y,self.size[0],self.size[1]]
               
    def tower_init(self,pos):
        self.x,self.y = pos
        self.image = common.images[self.img_name].copy()
        self.base = common.images["btn_base"].copy()
        self.base.blit(self.image,((Button.size[0]-self.size[0])/2,(Button.size[1]-self.size[1])/2))
        self.image = self.base
        self.rect = [self.x,self.y,Button.size[0],Button.size[1]]
        self.ghost = common.images[self.tower.img_name+"_active"]
        
    def click(self,mpos,user):        
        if common.test_posrect(mpos,self.rect):
            if user.money >= self.tower.cost:
                user.active_obj = self
            
            
class Target_Mode(Button):
    size = [40,16]        
    def __init__(self,pos,name):
        self.name = name
        self.img_name = name
        Button.__init__(self,pos)
        
    def click(self,mpos,user):
        if common.test_posrect(mpos,self.rect): 
            #if user.selected_obj <> None:
            user.selected_obj.targetmode = self.name
             
class Sell(Button):
    size = [60,20]
    img_name = 'sell'
    def __init__(self,pos):
        Button.__init__(self,pos)
        
    def click(self,mpos,user):
        if common.test_posrect(mpos,self.rect): 
            user.selected_obj.sell(user)
            user.twr_list.remove(user.selected_obj)
            user.selected_obj = None
            
class up(Button):
    name = 'up'
    img_name = "up"
    size = [32,8]
    def __init__(self,pos):
        Button.__init__(self,pos)
        
    def click(self,mpos,user):
        if common.test_posrect(mpos,self.rect):
            user.ui.btn_scroll(self.name)     

class down(Button):
    name = 'down'
    img_name = "down"
    size = [32,8]
    def __init__(self,pos):
        Button.__init__(self,pos)
        
    def click(self,mpos,user):
        if common.test_posrect(mpos,self.rect):
            user.ui.btn_scroll(self.name)
                          
class LWA(Button):
    tower = towers.LW_Arrow
    img_name = tower.img_name
    #size = [16,16]
    def __init__(self,pos):
        Button.tower_init(self,pos)
        
                
class HWA(Button):
    tower = towers.HW_Arrow
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)

class Slingshot(Button):
    tower = towers.Slingshot
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)

class Catapult(Button):
    tower = towers.Catapult
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)
        
class Spike(Button):
    tower = towers.Spike
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)
        
class Sludge(Button):
    tower = towers.Sludge
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)
        
class Fire(Button):
    tower = towers.Fire
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)
        
class Sniper(Button):
    tower = towers.Sniper
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)
               
class Rapidfire(Button):
    tower = towers.Rapidfire
    img_name = tower.img_name
    def __init__(self,pos):
        Button.tower_init(self,pos)

                
ui_btns = [Slingshot,LWA,HWA,Catapult,Spike,Sludge,Fire,Sniper,Rapidfire]
